package com.htl.exampro.exam.controller;

import com.htl.exampro.base.model.PageResult;
import com.htl.exampro.base.model.ServerResponse;
import com.htl.exampro.exam.pojo.query.ExamResultPageParam;
import com.htl.exampro.exam.pojo.vo.ExamResultVO;
import com.htl.exampro.exam.service.ExamResultService;
import com.htl.exampro.question.pojo.vo.AnswerDetailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 考试控制器
 */
@RestController
@RequestMapping("/exam-result")
public class ExamResultController {

    @Autowired
    private ExamResultService examResultService;

    @GetMapping("/{id}")
    public ServerResponse<ExamResultVO> getExamResultDetail(@PathVariable("id") String id) {
        ExamResultVO exam = examResultService.getExamResultDetail(id);
        return ServerResponse.success(exam);
    }

    @PostMapping("/history")
    public ServerResponse<PageResult<ExamResultVO>> getExamHistory(@RequestBody ExamResultPageParam pageParam) {
        PageResult<ExamResultVO> pageResult = examResultService.getExamHistory(pageParam);
        return ServerResponse.success(pageResult);
    }

    @PostMapping("/{examId}/registration")
    public ServerResponse<Integer> createExamResult(@PathVariable("examId") String examId) {
        int i = examResultService.createExamResult(examId);
        return ServerResponse.success(i);
    }

    @PostMapping("/{examId}/begin")
    public ServerResponse<Integer> beginExamResult(@PathVariable("examId") String examId) {
        int i = examResultService.beginExamResult(examId);
        return ServerResponse.success(i);
    }

    @PutMapping("/{examId}/submit")
    public ServerResponse<Integer> updateExamResult(@PathVariable("examId") String examId) {
        int i = examResultService.updateExamResult(examId);
        return ServerResponse.success(i);
    }

    /**
     * 获取考试统计信息和成绩分布
     */
    @GetMapping("/{examId}/results")
    public ServerResponse<Map<String, Object>> getExamResults(@PathVariable("examId") Long examId) {
        Map<String, Object> examResults = examResultService.getExamResults(examId);
        return ServerResponse.success(examResults);
    }

    /**
     * 分页获取成绩列表
     */
    @PostMapping("/scores")
    public ServerResponse<PageResult<ExamResultVO>> getExamScores(@RequestBody ExamResultPageParam pageParam) {
        return ServerResponse.success(examResultService.getExamScoresPage(pageParam));
    }

    /**
     * 获取某学生答题详情
     */
    @GetMapping("/{examId}/answer/{resultId}")
    public ServerResponse<List<AnswerDetailVO>> getExamAnswerDetail(@PathVariable("examId") Long examId,
                                                              @PathVariable("resultId") Long resultId) {
        List<AnswerDetailVO> answerPage = examResultService.getAnswerPage(examId, resultId);

        return ServerResponse.success(answerPage);
    }

}